﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Portal.Web.Utility
{
    public class ReturnPaging
    {
        public int StartIndex = 0;
        public int EndIndex = 0;
        public int TotalPage = 0;
        public int NoOfVisiblePage = 0;
        public int TotalSegment = 0;
        public string PagingText = "";
    }

    public class WebPaging
    {
        public static ReturnPaging GetPaging(
            int MaxItems,
            int CurrentPage,
            int ItemsPerRow,
            int NoOfVisiblePage,
            string JumpJavaScript)
        {
            return GetPaging(MaxItems, CurrentPage, ItemsPerRow, NoOfVisiblePage, JumpJavaScript, 1);
        }



        public static ReturnPaging GetPaging(
            int MaxItems,
            int CurrentPage,
            int ItemsPerRow,
            int NoOfVisiblePage,
            string JumpJavaScript,
            int ShowInfo)
        {
            ReturnPaging oReturnPaging = new ReturnPaging();


            if (NoOfVisiblePage == 0) NoOfVisiblePage = 5;
            if (ItemsPerRow == 0) ItemsPerRow = 25;
            int TotalPage = MaxItems / ItemsPerRow;
            if (MaxItems % ItemsPerRow != 0) TotalPage++;

            if (TotalPage <= 1)
            {
                oReturnPaging.StartIndex = 0;
                oReturnPaging.EndIndex = MaxItems;
                oReturnPaging.TotalPage = 1;
                oReturnPaging.NoOfVisiblePage = NoOfVisiblePage;
                oReturnPaging.TotalSegment = 0;
                if (ShowInfo == 1)
                {
                    oReturnPaging.PagingText =
                        "<div class=\"PageTitle\">Tổng số có " + MaxItems.ToString("#,#0") + " bản ghi tìm thấy</div>\r\n";
                }
                return oReturnPaging;
            }

            oReturnPaging.StartIndex = ItemsPerRow * CurrentPage;
            oReturnPaging.EndIndex = oReturnPaging.StartIndex + ItemsPerRow;
            if (oReturnPaging.EndIndex > MaxItems) oReturnPaging.EndIndex = MaxItems;

            int TotalSegment = TotalPage / NoOfVisiblePage;
            if (TotalPage % NoOfVisiblePage != 0) TotalSegment++;
            //doan nay tinh xem no nam o segment may
            int CurrentSegment = 0;
            for (int iSegment = 0; iSegment < TotalSegment; iSegment++)
            {
                int FromPage = iSegment * NoOfVisiblePage;
                int EndPage = FromPage + NoOfVisiblePage;
                if (EndPage > TotalPage) EndPage = TotalPage;
                if (CurrentPage >= FromPage && CurrentPage < EndPage)
                {
                    CurrentSegment = iSegment;
                    break;
                }
            }

            int StartPageIndex = CurrentSegment * NoOfVisiblePage;
            int EndPageIndex = StartPageIndex + NoOfVisiblePage;
            if (EndPageIndex > TotalPage) EndPageIndex = TotalPage;
            string Html = "";
            if (ShowInfo == 1)
            {
                Html +=
                    "<div class=PageTitle style=\"text-align:right;\">Tổng số có " + MaxItems.ToString("#,#0") + " bản ghi tìm thấy</div>\r\n";
            }
            Html +=
                    "<div class=\"pagination\">\r\n" +
                    "<ul>\r\n";

            if (CurrentPage > 0)
            {
                Html += "<li><a href=\"javascript:" + JumpJavaScript + "(0);\">«</a></li>";
            }
            else
            {
                Html += "<li><a>«</a></li>";
            }
            for (int iIndex = StartPageIndex; iIndex < EndPageIndex; iIndex++)
            {
                if (iIndex != CurrentPage)
                {
                    Html += "<li><a href=\"javascript:" + JumpJavaScript + "(" + iIndex + ");\">" + (iIndex + 1) + "</a></li>";
                }
                else
                {
                    Html += "<li class=\"disabled\"><a href=\"javascript:" + JumpJavaScript + "(" + iIndex + ");\">" + (iIndex + 1) + "</a></li>";
                }
            }
            if (CurrentPage < TotalPage - 1)
            {
                Html += "<li><a href=\"javascript:" + JumpJavaScript + "(" + (CurrentPage + 1) + ");\">»</a></li>";
            }
            else
            {
                Html += "<li><a>»</a></li>";
            }
            Html += "</ul>" +
                    "</div>\r\n";
            oReturnPaging.PagingText = Html;
            oReturnPaging.TotalPage = TotalPage;
            oReturnPaging.NoOfVisiblePage = NoOfVisiblePage;
            oReturnPaging.TotalSegment = TotalSegment;
            return oReturnPaging;
        }
    }
}
